package io.finer.erp.jeecg.bas.service.impl;

import io.finer.erp.jeecg.bas.entity.ZrkDeliver;
import io.finer.erp.jeecg.bas.entity.ZrkDeliverEntry;
import io.finer.erp.jeecg.bas.mapper.ZrkDeliverEntryMapper;
import io.finer.erp.jeecg.bas.mapper.ZrkDeliverMapper;
import io.finer.erp.jeecg.bas.service.IZrkDeliverService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.api.client.util.Maps;

import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * @Description: 销售出货单
 * @Author: jeecg-boot
 * @Date:   2020-10-21
 * @Version: V1.0
 */
@Service
public class ZrkDeliverServiceImpl extends ServiceImpl<ZrkDeliverMapper, ZrkDeliver> implements IZrkDeliverService {

	@Autowired
	private ZrkDeliverMapper zrkDeliverMapper;
	@Autowired
	private ZrkDeliverEntryMapper zrkDeliverEntryMapper;
	
	@Override
	@Transactional
	public void saveMain(ZrkDeliver zrkDeliver, List<ZrkDeliverEntry> zrkDeliverEntryList) {
		zrkDeliverMapper.insert(zrkDeliver);
		if(zrkDeliverEntryList!=null && zrkDeliverEntryList.size()>0) {
			for(ZrkDeliverEntry entity:zrkDeliverEntryList) {
				//外键设置
				entity.setDeliverId(zrkDeliver.getId());
				zrkDeliverEntryMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(ZrkDeliver zrkDeliver,List<ZrkDeliverEntry> zrkDeliverEntryList) {
		zrkDeliverMapper.updateById(zrkDeliver);
		
		//1.先删除子表数据
		zrkDeliverEntryMapper.deleteByMainId(zrkDeliver.getId());
		
		//2.子表数据重新插入
		if(zrkDeliverEntryList!=null && zrkDeliverEntryList.size()>0) {
			for(ZrkDeliverEntry entity:zrkDeliverEntryList) {
				//外键设置
				entity.setDeliverId(zrkDeliver.getId());
				zrkDeliverEntryMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		zrkDeliverEntryMapper.deleteByMainId(id);
		zrkDeliverMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			zrkDeliverEntryMapper.deleteByMainId(id.toString());
			zrkDeliverMapper.deleteById(id);
		}
	}

	@Override
	public Result<?> getBillShipmentList(String billNo) {
		LambdaQueryWrapper<ZrkDeliver> query = new LambdaQueryWrapper<>();
		query.eq(ZrkDeliver::getBillNo, billNo);
		List<ZrkDeliver> list = list(query);
		return Result.ok(list);
	}

	@Override
	public Map<String, String> getShipmentByCode(String code) {
		// TODO Auto-generated method stub
		Map<String, String> map = Maps.newLinkedHashMap();
		ZrkDeliver deliver = this.getByCode(code);
		if(deliver == null) {
			map.put("id", "0");
		} else {
			map.put("id", deliver.getId());
		}
		
		return map;
	}
	
	private ZrkDeliver getByCode(String code) {
		LambdaQueryWrapper<ZrkDeliver> query = new LambdaQueryWrapper<ZrkDeliver>();
		query.eq(ZrkDeliver::getDeliverCode, code);
		return getOne(query);
	}
}
